<template>
  <div class="update-gender">
    <!-- 导航栏 -->
    <van-picker
      show-toolbar
      title="修改性别"
      :columns="columns"
      :default-index="value"
      @cancel="$emit('close')"
      @confirm="onConfirm"
      @change="onPickerChange"
    />
    <!-- changge事件得到他是男是女的索引 -->
  </div>
</template>

<script>
import { updateUserName } from "@/api/user.js";

export default {
  name:'UpdateGender',
  data() {
    return {
      columns: ["男", "女"],
      localGender: this.value,
    };
  },
  methods: {
    //   点击完成事件
    async onConfirm() {
      this.$toast.loading({
        message: "修改中",
        forbidClick: true, //背景禁止点击
        duration: 0, //持续展示
      });
      try {
        const localGender = this.localGender;
        const { data } = await updateUserName({
          gender: localGender,
          // 修改谁就写谁
        });
        console.log(data.data);
        // v-modle触发input事件
        // 更新视图
        this.$emit("input", localGender);
        // 关闭弹层
        this.$emit("close");
        // 提示成功;
        this.$toast.success("更新成功");
      } catch (err) {
        this.$toast.fail("性别更新失败");
      }
    },
    onPickerChange(picker, value, index) {
      this.localGender = index;
     
    },
  },
  props: {
    value: {
      type: Number,
      required: true,
    },
  },
};
</script>

<style>
</style>